﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ModelResourceManager.ResourceModel
{
    public class ResourceManager
    {
        private readonly EntityResourceManager _entityManager;

        public ResourceManager()
        {
            _entityManager = new EntityResourceManager();
        }

        public bool Add(ResourceDTO resourceDto)
        {
            if (resourceDto == null)
                return false;
            Resource resource = ParseToResource(resourceDto);
            if (resource == null)
                return false;
            bool result = _entityManager.Add(resource);
            return result;
        }

        public bool Update(ResourceDTO resourceDto)
        {
            if (resourceDto == null)
                return false;
            Resource resource = ParseToResource(resourceDto);
            if (resource == null)
                return false;
            var result = _entityManager.Update(resource);
            return result;
        }

        public bool Delete(Guid id)
        {
            var result = _entityManager.Delete(id);
            return result;
        }

        public bool SetStatus(Guid id, bool _lock)
        {
            bool result =_entityManager.SetStatus(id, _lock);
            return result;
        }

        public ResourceDTO GetCurrentResource(Guid id)
        {
            Resource resource = _entityManager.GetCurrentResource(id);
            ResourceDTO resourceDto = ParseToResourceDto(resource);
            return resourceDto;
        }

        public List<ResourceDTO> GetAll(bool activeOnly = false)
        {
            IEnumerable<Resource> resources = _entityManager.GetAll(activeOnly);
            if (resources == null || resources.Any())
                return null;
            List<ResourceDTO> resourceDtos = resources.Select(ParseToResourceDto).ToList();
            return resourceDtos;
        }

        public string GetResourceName(Guid id)
        {
            string name = _entityManager.GetResourceName(id);
            return name;
        }

        public Guid GetResourceId(string name)
        {
            var id = _entityManager.GetResourceId(name);
            return id;
        }

        private Resource ParseToResource(ResourceDTO resourceDto)
        {
            return new Resource()
                {
                    ResourceId = resourceDto.ResourceId,
                    Name = resourceDto.Name,
                    StartTime = resourceDto.StartTime,
                    EndTime = resourceDto.EndTime,
                    Locked = resourceDto.Locked,
                    TimeOfUsing = resourceDto.TimeOfUsing,
                    UserCount = resourceDto.UserCount,
                    OnlyAutoTime = resourceDto.OnlyAutoTime
                };
        }

        private ResourceDTO ParseToResourceDto(Resource resource)
        {
            return new ResourceDTO()
            {
                ResourceId = resource.ResourceId,
                Name = resource.Name,
                StartTime = resource.StartTime,
                EndTime = resource.EndTime,
                Locked = resource.Locked,
                TimeOfUsing = resource.TimeOfUsing,
                UserCount = resource.UserCount,
                OnlyAutoTime = resource.OnlyAutoTime
            };
        }
    }
}
